package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.Cost;
import com.ruoyi.inventory.service.ICostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 其他成本Controller
 * 
 * @author xwj
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/inventory/cost")
@Api(tags = "其他成本")
public class CostController extends BaseController {
    @Autowired
    private ICostService costService;

    /**
     * 查询其他成本列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Cost cost) {
        startPage();
        List<Cost> list = costService.selectCostList(cost);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Cost cost) {
        startOrderBy();
        List<Cost> list = costService.selectCostList(cost);
        return success(list);
    }

    /**
     * 导出其他成本列表
     */
    @Log(title = "其他成本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Cost cost) {
        List<Cost> list = costService.selectCostList(cost);
        ExcelUtil<Cost> util = new ExcelUtil<Cost>(Cost.class);
        util.exportExcel(response, list, "其他成本数据");
    }

    /**
     * 获取其他成本详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(costService.selectCostById(id));
    }

    /**
     * 新增其他成本
     */
    @Log(title = "其他成本", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Cost cost) {
        SysUser user = getLoginUser().getUser();
        if(cost.getId() == null) {
        cost.setCreateId(user.getUserId());
            cost.setCreateTime(DateUtils.getNowDate());
            cost.setCreateBy(user.getNickName());
            costService.insertCost(cost);
        } else {
            cost.setUpdateId(user.getUserId());
            cost.setUpdateTime(DateUtils.getNowDate());
            cost.setUpdateBy(user.getNickName());
            costService.updateCost(cost);
        }

        return success(cost);
    }

    /**
     * 修改其他成本
     */
    @Log(title = "其他成本", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Cost cost) {
        SysUser user = getLoginUser().getUser();
                cost.setUpdateId(user.getUserId());
                cost.setUpdateTime(DateUtils.getNowDate());
                cost.setUpdateBy(user.getNickName());
        return success(costService.updateCost(cost));
    }

    /**
     * 删除其他成本
     */
    @Log(title = "其他成本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(costService.deleteCostByIds(ids));
    }

    @Log(title = "其他成本", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Cost cost) {
        int num = costService.delete(cost);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Cost>> queryPage(@RequestBody Cost cost) {
        Pages<Cost> page = costService.queryPage(cost);
        return ResponseModel.of(page);
    }
}
